<template>
  <div class="w">
    <div class="top">
      <i class="iconfont icon-xiangzuojiantou" @click="Jump ('login')"></i>
      <p>注册</p>
    </div>
    <van-form @submit="onSubmit">
      <van-field
        v-model="username"
        name="手机号"
        label="手机号"
        placeholder="手机号"
        autocomplete="off"
        :rules="[{ required: true,
           message: '请填正确格式的手机号',
           pattern: /^(13[0-9]|14[01456879]|15[0-35-9]|16[2567]|17[0-8]|18[0-9]|19[0-35-9])\d{8}$/,
           }]"
            />
      <van-field
        v-model="sms"
        center
        clearable
        label="验证码"
        placeholder="请输入验证码"
        autocomplete="new-password"
        :rules="[{ required: true, message: '请输入验证码' }]"
      >
      <template #button>
        <van-button
        size="small"
        type="default"
        color="#ececec"
        native-type="button"
        round
        @click="SendSms"
        ref="sms"
        :disabled="disable"
        >发送验证码</van-button>
      </template>
      </van-field>
      <van-field
        v-model="pas"
        type="password"
        name="设置密码"
        label="设置密码"
        placeholder="设置密码"
        :rules="[{
         required: true,
         message: '密码必须由数字、字母两种字符组成，长度在8-15位之间',
         pattern:/^(?![0-9]+$)(?![a-zA-Z]+$)[0-9A-Za-z]{8,15}$/}]"
        autocomplete="new-password"
      />
      <van-field
        v-model="password"
        type="password"
        name="确认密码"
        label="确认密码"
        placeholder="确认密码"
        :rules="[{ validator , message: '密码不一致' }]"
        autocomplete="new-password"
      />
      <div class="checkboxx">
        <van-checkbox v-model="checked" shape="square" icon-size="10px" :rules="[{ required: true, message: '勾选我已阅读并同意' }]">
      我已阅读并同意《用户使用协议》，《隐私权条款》
      </van-checkbox>
      </div>
      <div style="margin: 16px;">
        <van-button round block type="info" native-type="submit" color="#ff4c4c">提交</van-button>
      </div>
    </van-form>
  </div>
</template>

<script>
import Vue from 'vue'
import { Toast } from 'vant'
import { Sms, Reg } from '@/api/User/index'
Vue.use(Toast)
export default {
  data () {
    return {
      username: '',
      sms: '',
      pas: '',
      password: '',
      checked: '',
      timi: 60,
      disable: false,
      ervalsms: ''
    }
  },
  methods: {
    // 发送验证码
    async SendSms () {
      if (/^(13[0-9]|14[01456879]|15[0-35-9]|16[2567]|17[0-8]|18[0-9]|19[0-35-9])\d{8}$/.test(this.username)) {
        await Sms(this.username)
        this.ervalsms = setInterval(() => {
          this.disable = true
          if (this.timi <= 0) {
            clearInterval(this.ervalsms)
            this.timi = 60
            this.disable = false
            this.$refs.sms.innerText = '发送验证码'
          }
          this.$refs.sms.innerText = `${this.timi}秒后重新获取`
          this.timi--
        }, 1000)
      }
    },
    // 两次密码校验
    validator (val) {
      return this.pas === val
    },
    // 发送注册请求
    async onSubmit () {
      if (!this.checked) return Toast('请勾选用户使用协议')
      const res = {
        phone: this.username,
        code: this.sms,
        pwd: this.pas
      }
      const data = await Reg(res)
      // 显示请求信息
      Toast(data.info)
      // 注册成功跳转登录页面
      if (data.success) this.$router.push('/login')
    },
    Jump (url) {
      this.$router.replace({ path: url })
    }
  },
  beforeDestroy () {
    clearInterval(this.ervalsms)
    this.$refs.sms.innerText = '发送验证码'
  }
}
</script>

<style lang="less" scoped>
.w{
  width: 100%;
  height: 100%;
  position: absolute;
  top: 0px;
  bottom: 0px;
  background-color: #ffffff;
  display: flex;
  flex-direction: column;
  align-items: center;
  .top{
    width: 100%;
    height: 100px;
    display: flex;
    justify-content: center;
    align-items: center;
    background-color: #ffffff;
    p{
      font-size: 35px;
      margin: 25px;
    }
    >.icon-xiangzuojiantou{
      position: relative;
      left: -250px;
      font-size: 30px;
    }
  }
  .checkboxx{
    box-sizing: border-box;
    padding-left: 80px;
    color: #666666;
    font-size: 23px;
    margin-top: 500px;
  }
}
</style>
